﻿namespace HiMap.MapControls
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Reflection;
    using System.Windows.Forms;

    internal class PicCtrl
    {
        private Color _DrawColor = Color.Red;
        private MapControl _Map;
        private List<IPoint> _MapPoints;
        private List<PictureBox> _Pics;
        private List<Point> _Points;
        private PicType _Type;
        private bool isMouseDown;
        private int NameIndex;
        private Image oldImage;
        private int PicStaX;
        private int PicStaY;
        private PictureBox SelectedPic;

        public PicCtrl(MapControl Map, List<IPoint> MapPoints, List<Point> Points, PicType Type)
        {
            this._Map = Map;
            this._MapPoints = MapPoints;
            this._Points = Points;
            this._Type = Type;
            this.NameIndex = 0;
            this.isMouseDown = false;
            this._Pics = new List<PictureBox>();
            this.oldImage = (Image) Map.MapImage.Clone();
            this.SelectedPic = null;
        }

        public void Clear(bool isSave)
        {
            for (int i = this._Map.Controls.get_Count() - 1; i >= 0; i--)
            {
                if (((this._Map.Controls[i].GetType() == typeof(PictureBox)) && (this._Map.Controls[i].Name.Length >= 4)) && (this._Map.Controls[i].Name.Substring(0, 4) == "Temp"))
                {
                    this._Map.Controls.RemoveAt(i);
                }
            }
            this.NameIndex = 0;
            this._Pics.Clear();
            if (!isSave)
            {
                this._Map.MapImage = this.oldImage;
                this._Map.Refresh();
            }
        }

        public void CreatePic(int x, int y)
        {
            if (this.picType != PicType.Null)
            {
                if (this.picType == PicType.Point)
                {
                    this._MapPoints.Clear();
                    this._Points.Clear();
                    this.Clear(false);
                }
                PictureBox box = new PictureBox();
                box.Name = "TempPic" + this.NameIndex.ToString();
                box.Visible = false;
                box.Width = 14;
                box.Height = 14;
                box.BackColor = this.DrawColor;
                box.Location = new Point(x - 8, y - 8);
                box.MouseDown += new MouseEventHandler(this.PicMouseDown);
                box.MouseMove += new MouseEventHandler(this.PicMouseMove);
                box.MouseUp += new MouseEventHandler(this.PicMouseUp);
                this._Map.Controls.Add(box);
                this._Map.Controls.SetChildIndex(box, 0);
                this.NameIndex++;
                box.Visible = true;
                this._Pics.Add(box);
            }
        }

        public void Delete(int index)
        {
            if ((this.SelectedPic != null) && (this.SelectedPic.Name == this._Pics.get_Item(index).Name))
            {
                this.SelectedPic = null;
            }
            this._Map.Controls.Remove(this._Pics.get_Item(index));
            this._Pics.RemoveAt(index);
            this._MapPoints.RemoveAt(index);
            this._Points.RemoveAt(index);
            this.Draw();
        }

        public void Dispose()
        {
        }

        public void Draw()
        {
            switch (this._Type)
            {
                case PicType.Point:
                case PicType.MultiPoint:
                    break;

                case PicType.Polyline:
                    this.Drawline();
                    return;

                case PicType.Polygon:
                    this.DrawPolygon();
                    break;

                default:
                    return;
            }
        }

        private void Drawline()
        {
            if (this._Points.get_Count() >= 2)
            {
                this._Map.MapImage = (Image) this.oldImage.Clone();
                Point[] pts = new Point[this._Points.get_Count()];
                for (int i = 0; i < this._Points.get_Count(); i++)
                {
                    pts[i] = new Point();
                    pts[i].X = this._Points.get_Item(i).X;
                    pts[i].Y = this._Points.get_Item(i).Y;
                }
                this._Map.DrawLine(new Pen(Color.FromArgb(0x80, 0xff, 0xff)), pts);
            }
        }

        private void DrawPolygon()
        {
            if (this._Points.get_Count() >= 3)
            {
                this._Map.MapImage = (Image) this.oldImage.Clone();
                Point[] pts = new Point[this._Points.get_Count()];
                for (int i = 0; i < this._Points.get_Count(); i++)
                {
                    pts[i] = new Point();
                    pts[i].X = this._Points.get_Item(i).X;
                    pts[i].Y = this._Points.get_Item(i).Y;
                }
                this._Map.DrawPolygon(new Pen(Color.FromArgb(0x80, 0xff, 0xff)), new SolidBrush(Color.FromArgb(0x80, 0xff, 0xff)), pts);
            }
        }

        public void InsertPic(int index, int x, int y)
        {
            if (this.picType != PicType.Null)
            {
                if (this.picType == PicType.Point)
                {
                    this._MapPoints.Clear();
                    this._Points.Clear();
                    this.Clear(false);
                }
                PictureBox box = new PictureBox();
                box.Name = "TempPic" + this.NameIndex.ToString();
                box.Visible = false;
                box.Width = 14;
                box.Height = 14;
                box.BackColor = this.DrawColor;
                box.Location = new Point(x - 8, y - 8);
                box.MouseDown += new MouseEventHandler(this.PicMouseDown);
                box.MouseMove += new MouseEventHandler(this.PicMouseMove);
                box.MouseUp += new MouseEventHandler(this.PicMouseUp);
                this._Map.Controls.Add(box);
                this._Map.Controls.SetChildIndex(box, 0);
                this.NameIndex++;
                box.Visible = true;
                this._Pics.Insert(index, box);
            }
        }

        public void MovePic(int index)
        {
            this._Pics.get_Item(index).Location = new Point(this._Points.get_Item(index).X - 8, this._Points.get_Item(index).Y - 8);
            this.Draw();
        }

        private void PicMouseDown(object sender, MouseEventArgs e)
        {
            this.isMouseDown = true;
            this.PicStaX = e.X;
            this.PicStaY = e.Y;
        }

        private void PicMouseMove(object sender, MouseEventArgs e)
        {
            PictureBox box = (PictureBox) sender;
            if (this.isMouseDown)
            {
                box.Location = new Point((box.Location.X + e.X) - this.PicStaX, (box.Location.Y + e.Y) - this.PicStaY);
            }
        }

        private void PicMouseUp(object sender, MouseEventArgs e)
        {
            PictureBox box = (PictureBox) sender;
            this._MapPoints.set_Item(this._Pics.IndexOf(box), this._Map.PointToMapPoint(box.Location.X + 8, box.Location.Y + 8));
            this._Points.set_Item(this._Pics.IndexOf(box), new Point(box.Location.X + 8, box.Location.Y + 8));
            this.isMouseDown = false;
            this.Draw();
        }

        public void Selected(int index)
        {
            if (this.SelectedPic != null)
            {
                this.SelectedPic.BackColor = this.DrawColor;
            }
            this.SelectedPic = this._Pics.get_Item(index);
            this.SelectedPic.BackColor = Color.Blue;
        }

        public Color DrawColor
        {
            get
            {
                return this._DrawColor;
            }
            set
            {
                this._DrawColor = value;
            }
        }

        public Image Img
        {
            get
            {
                return this.oldImage;
            }
            set
            {
                if (this.oldImage != null)
                {
                    this.oldImage.Dispose();
                }
                this.oldImage = value;
            }
        }

        public PictureBox this[int index]
        {
            get
            {
                return this._Pics.get_Item(index);
            }
        }

        public PicType picType
        {
            get
            {
                return this._Type;
            }
            set
            {
                this._Type = value;
            }
        }
    }
}

